package org.jeecg.websocket;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.web.bind.annotation.*;

import java.util.*;

/**
 * @Description: 设备Api接口
 * @Author: jeecg-boot
 * @Date:   2019-08-22
 * @Version: V1.0
 */
@Slf4j
@RestController
@RequestMapping("/api/socketTest")
public class ApiSendController {

	@Autowired
	SocketIOService socketIOService;

	@PostMapping(value = "/apiSend")
	public String apiSend(@RequestParam("userId") String userId, @RequestParam("event") String event) throws Exception {
		// 返回值不是json, 而是成功发送给客户端的设备数量
		int res = 0;
		if (StringUtils.isNotBlank(userId)){
			String userIds[] = userId.split(";");
			for (int i=0;i<userIds.length;i++){
				res += socketIOService.pushMessageToUser(userIds[i],event,"{'test1111',111222}");
			}
		}
		return res+"";
//		return ""+socketIOService.pushMessageToUser(userId,event,"{'test1111',111222}");
	}

	@RequestMapping(value = "/", method = RequestMethod.GET)
	public String printServiceB() {
		return "sayHello";
	}

}
